<template>
	<view class="content">

<van-button type="primary">主要按钮</van-button>
		<image :src="codeUrl" @click="getCode" class="login-code-img" />
		<input type="text" v-model="loginForm.code" placeholder="请输入验证码" />
		<button type="default" @click="onLogin">登录</button>
		<button type="default" @click="onGetUserInfo">获取用户信息</button>
	</view>
</template>

<script setup>
  import { Button } from 'vant';

	import {
		getCodeImg,
		login,
		getInfo
	} from "@/api/login";
	import {
		getToken
	} from '@/utils/auth'

	const store = useStore();
	const {
		proxy
	} = getCurrentInstance();

	// import { encrypt, decrypt } from "@/utils/jsencrypt";

	let codeUrl = ref("")
	// 验证码开关
	const captchaOnOff = ref(true);
	const loginForm = ref({
		username: "admin",
		password: "admin123",
		rememberMe: false,
		code: "",
		uuid: ""
	});
	const getCode = () => {
		getCodeImg().then(res => {
			captchaOnOff.value = res.captchaOnOff === undefined ? true : res.captchaOnOff;
			if (captchaOnOff.value) {
				codeUrl.value = "data:image/gif;base64," + res.img;
				loginForm.value.uuid = res.uuid;
			}
		})
	}

	const onLogin = () => {
		debugger
		// 调用action的登录方法
		store.dispatch("Login", loginForm.value).then(() => {

			uni.showToast({
				title: '登录成功',
				duration: 2000
			})
		}).catch(() => {
			// 重新获取验证码
			if (captchaOnOff.value) {
				getCode();
			}
		});
		// login(loginForm.value).then(res => {
		//         setToken(res.token)

		// 	uni.showToast({
		// 		success: "成功"

		// 	})
		// })

	}
	const onGetUserInfo = () => {
		debugger
		getInfo().then(res => {
			uni.showToast({
				title: '用户信息获取成功',
				duration: 2000
			})
		})
	}
	getCode()
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
